package com.xuelang.pipeline.infrastructure.pipeline.repository;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xuelang.pipeline.client.common.result.PageRecord;
import com.xuelang.pipeline.client.pipeline.dto.PipelineTagQuery;
import com.xuelang.pipeline.domain.pipeline.PipelineTag;
import com.xuelang.pipeline.domain.pipeline.repository.PipelineTagRepository;
import com.xuelang.pipeline.infrastructure.pipeline.converter.PipelineTagToPipelineTagDOConverter;
import com.xuelang.pipeline.infrastructure.pipeline.dataobject.PipelineTagDO;
import com.xuelang.pipeline.infrastructure.pipeline.mapper.PipelineTagMapper;
import dm.jdbc.util.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.compress.utils.Lists;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 流水线标签仓库实现类
 *
 * @author xiwen.zxg
 * @since 2024/5/15
 */
@Slf4j
@Repository
public class PipelineTagRepositoryImpl implements PipelineTagRepository {

    @Autowired
    PipelineTagMapper pipelineTagMapper;

    @Override
    public PageRecord<PipelineTag> pageList(long currentPage, long pageSize, PipelineTag pipelineTag) {
        currentPage = currentPage < 1 ? 1 : currentPage;
        pageSize = pageSize < 1 ? 10 : pageSize;
        Page<PipelineTagDO> page = new Page<>(currentPage, pageSize);
        LambdaQueryWrapper<PipelineTagDO> wrapper = Wrappers.lambdaQuery(PipelineTagDO.class);
        if (pipelineTag != null) {
            if (StringUtils.isNotBlank(pipelineTag.getPipelineCode())) {
                wrapper.eq(PipelineTagDO::getPipelineCode, pipelineTag.getPipelineCode());
            }
        }
        pipelineTagMapper.selectPage(page, wrapper);
        return new PageRecord<>(
                page.getCurrent(),
                page.getSize(),
                page.getTotal(),
                page.getPages(),
                page.getRecords().stream().map(PipelineTagToPipelineTagDOConverter.INSTANCE::pipelineTagDOToPipelineTag).collect(Collectors.toList())
        );
    }

    @Override
    public List<PipelineTag> list(PipelineTag pipelineTag) {
        LambdaQueryWrapper<PipelineTagDO> wrapper = Wrappers.lambdaQuery(PipelineTagDO.class);
        if (pipelineTag != null) {
            if (StringUtils.isNotBlank(pipelineTag.getPipelineCode())) {
                wrapper.eq(PipelineTagDO::getPipelineCode, pipelineTag.getPipelineCode());
            }
        }
        return pipelineTagMapper.selectList(wrapper).stream()
                .map(PipelineTagToPipelineTagDOConverter.INSTANCE::pipelineTagDOToPipelineTag).collect(Collectors.toList());
    }


    @Override
    public Boolean save(PipelineTag pipelineTag) {
        return pipelineTagMapper.insert(PipelineTagToPipelineTagDOConverter.INSTANCE.pipelineTagToPipelineTagDO(pipelineTag)) == 1;
    }

    @Override
    public Boolean update(PipelineTag pipelineTag) {
        return pipelineTagMapper.update(
                PipelineTagToPipelineTagDOConverter.INSTANCE.pipelineTagToPipelineTagDO(pipelineTag),
                Wrappers.lambdaQuery(PipelineTagDO.class).eq(PipelineTagDO::getId, pipelineTag.getId())) == 1;
    }

    @Override
    public Boolean delete(String uuid) {
        return pipelineTagMapper.delete(Wrappers.lambdaQuery(PipelineTagDO.class).eq(PipelineTagDO::getId, uuid)) == 1;
    }

    @Override
    public PipelineTag query(String uuid) {
        PipelineTagDO pipelineTagDO = pipelineTagMapper.selectOne(Wrappers.lambdaQuery(PipelineTagDO.class).eq(PipelineTagDO::getId, uuid));
        return PipelineTagToPipelineTagDOConverter.INSTANCE.pipelineTagDOToPipelineTag(pipelineTagDO);
    }

    @Override
    public List<PipelineTag> queryByPipelineCode(String pipelineCode) {
        if (StringUtil.isEmpty(pipelineCode)) {
            return null;
        }
        LambdaQueryWrapper<PipelineTagDO> wrapper = Wrappers.lambdaQuery(PipelineTagDO.class);
        wrapper.eq(PipelineTagDO::getPipelineCode, pipelineCode);
        List<PipelineTagDO> wrapperResult = pipelineTagMapper.selectList(wrapper);
        if (CollectionUtils.isEmpty(wrapperResult)) {
            return null;
        }

        List<PipelineTag> result = wrapperResult.stream()
                .map(PipelineTagToPipelineTagDOConverter.INSTANCE::pipelineTagDOToPipelineTag)
                .collect(Collectors.toList());
        return result;
    }

    @Override
    public Boolean deleteByPipelineCode(String pipelineCode) {
        pipelineTagMapper.delete(Wrappers.lambdaQuery(PipelineTagDO.class).eq(PipelineTagDO::getPipelineCode, pipelineCode));
        return true;
    }

    @Override
    public Boolean modifyPipelineTags(PipelineTagQuery query, String creatorId) {
        String pipelineCode = query.getPipelineCode();
        List<String> tagUuidList = query.getTagUuidList();
        // 查询现有关联关系并删除
        LambdaQueryWrapper<PipelineTagDO> wrapper = Wrappers.lambdaQuery(PipelineTagDO.class);
        wrapper.eq(PipelineTagDO::getPipelineCode, pipelineCode);
        // 删除
        pipelineTagMapper.delete(wrapper);

        // 若分组为空，则直接返回
        if(CollectionUtils.isEmpty(tagUuidList)){
            return true;
        }

        // 整理批量插入的实体
        List<PipelineTagDO> toAddList = Lists.newArrayList();
        for(String tagUuid : tagUuidList){
            PipelineTagDO toAdd = new PipelineTagDO();
            toAdd.setPipelineCode(pipelineCode);
            toAdd.setTagUuid(tagUuid);
            toAdd.setCreatorId(creatorId);
            toAdd.setCreateTime(new Date());
            toAddList.add(toAdd);
        }

        // 更新关联关系
        return pipelineTagMapper.insertBatch(toAddList) != 0;
    }
}
